package com.example.cacheservice.service.impl;

import cn.hutool.json.JSONUtil;
import com.example.cacheservice.common.entity.User;
import com.example.cacheservice.dao.UserMapper;
import com.example.cacheservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

/**
 * @author wuxinxin
 */
@CacheConfig(cacheNames = {"user"})
@Service
public class UserServiceImpl implements UserService {

    public UserServiceImpl() {
        System.out.println("创建UserServiceImpl");
    }

    @Autowired
    private UserMapper userMapper;


    //@Cacheable(cacheNames = {"user"})
    @Cacheable
    @Override
    public User getUserById(Integer id) {
        System.out.println(String.format("查询id=%s的用户",id));
        if(id==4){
            int j=1/0;
        }
        return userMapper.selectById(id);
    }

    @CachePut(cacheNames = {"user"},key ="#user.id")
    @Override
    public User updateUserById(User user) {
        System.out.println(String.format("更新数据=%s", JSONUtil.toJsonStr(user)));
         userMapper.updateById(user);
         return user;
    }

    @Override
    @CacheEvict(cacheNames = {"user"})
    public void deleteUserById(Integer id){
        System.out.println(String.format("删除id=%s的用户",id));
    }


}
